var zTreeUtil2 = {
	onCheck: function(e, treeId, treeNode, isClick){
		var zTree = $.fn.zTree.getZTreeObj(treeId);
		isClick && zTree.checkNode(treeNode);
		var nodes = zTree.getCheckedNodes(true);
		var code_array = [];
		var name_array = [];
		for(var i=0; i<nodes.length; i++) {
			//if(!nodes[i].getCheckStatus().half) {
				code_array.push(nodes[i].code);
				name_array.push(nodes[i].name);
			//}
		}
		var _id = treeId.substring('select_tree_check_'.length);
		$(".select_" + _id).val(name_array.join(","));
		$(".hidden_" + _id).val(code_array.join(","));
	},
	setting: {
		check: { enable: true, chkStyle: "checkbox", chkboxType: { "Y": "ps", "N": "ps" }},
		view: { dblClickExpand: false },
		data: { simpleData: { enable: true } },
		callback: { 
			onClick: function(e, treeId, treeNode, isClick) {
				zTreeUtil2.onCheck(e, treeId, treeNode, true);
			},
			onCheck: function(e, treeId, treeNode) {
				zTreeUtil2.onCheck(e, treeId, treeNode);
			}
		}
	},
	getPre: function(name) {
		if(!!name) {
			name = name.substring(0, name.lastIndexOf("_") + 1);
		}
		return name || "";
	},
	showMenu: function(entity) {
		var cityObj = $(".select_" + entity);
		var cityOffset = cityObj.offset();
		$(".select_tree_menu_" + entity).css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");
		$("body").bind("mousedown", this.onBodyDown);
		var nodes = $(".hidden_" + entity).val();
		if(nodes) {
			var zTree = $.fn.zTree.getZTreeObj("select_tree_check_" + entity);
			$(nodes.split(",")).each(function(){
				var node = zTree.getNodeByParam("code", "" + this, null);
				node && zTree.checkNode(node, true);
			});
		}
	},
	onBodyDown: function(event) {
		var that = this;
		if (!($(event.target).parents(".menuContent").length>0)) {
			$(".menuContent").fadeOut("fast");
			$("body").unbind("mousedown", that.onBodyDown);
		}
	},
	/**
	 * 初始化
	 * @param id input输入框的id，select_实体名。例如 select_fiscal
	 * @param nodesData 树的数据，json格式
	 */
	init: function(id, nodesData){
		var that = this;
		var entity = id.substring("select_".length);
		var treeId = "select_tree_check_" + entity;
		$.fn.zTree.init($("#" + treeId), that.setting, nodesData);
		$("." + id).click(function(){
			that.showMenu(entity);
		});
		var nodes = $(".hidden_" + entity).val();
		if(nodes) {
			var zTree = $.fn.zTree.getZTreeObj("select_tree_check_" + entity);
			var nameArr = [];
			$(nodes.split(",")).each(function(){
				var node = zTree.getNodeByParam("code", "" + this, null);
				nameArr.push(node.name);
			});
			$(".select_" + entity).val(nameArr.join(","));
		}
	}
}